﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class TinhTrangDetailView : Form
    {
        public event EventHandler SaveChanged;
        public event EventHandler DeleteChanged;
        private TinhTrang _tinhTrang;
        private BindingList<TinhTrang> bindingListTinhTrang;
        
        public TinhTrangDetailView()
        {
            InitializeComponent();
        }
        public TinhTrangDetailView(TinhTrang tinhTrang)
        {
            InitializeComponent();

            bindingListTinhTrang = new BindingList<TinhTrang>();
            _tinhTrang = tinhTrang;
            bindingListTinhTrang.Add(_tinhTrang);

            bindingSourceTinhTrang.DataSource = bindingListTinhTrang;
            layoutControl1.Refresh();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Luu();
            
            
        }
        #region User Functions
        void OnSaveChange()
        {
            if (SaveChanged != null)
                SaveChanged(this, EventArgs.Empty);
        }
        void OnDeleteChange()
        {
            if (DeleteChanged != null)
                DeleteChanged(this, EventArgs.Empty);
        }
        void Luu()
        {
            if (_tinhTrang.Oid == -1)
            {
                DataAccessObject.TinhTrang.Insert(_tinhTrang);
                OnSaveChange();
            }
            else
                DataAccessObject.TinhTrang.Update(_tinhTrang);
           
        }
        void Xoa()
        {
            if (_tinhTrang.Oid <= 0 || DataAccessObject.TinhTrang.Delete(_tinhTrang) <= 0)
            {
                MessageBox.Show("Không xóa được thông tin tình trạng này", "Lưu ý", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            OnDeleteChange();
        }
        #endregion

        private void LuuVaDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Luu();
            this.Close();   
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Xoa();
        }
    }
}
